package cn.HuanG.org.service.impl;

import cn.HuanG.annotation.OtherLog;
import cn.HuanG.org.domain.Product;
import cn.HuanG.org.domain.ProductDetail;
import cn.HuanG.org.mapper.ProductDetailMapper;
import cn.HuanG.org.mapper.ProductMapper;
import cn.HuanG.org.query.ProductQuery;
import cn.HuanG.org.service.IProductService;
import cn.HuanG.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Override
    @Transactional
    @OtherLog(logName = "增加")
    public void save(Product product) {
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProduct_id(product.getId());
        productDetailMapper.save(detail);
    }

    @Override
    @Transactional
    @OtherLog(logName = "修改")
    public void update(Product product) {
        productMapper.update(product);
    }

    @Override
    @Transactional
    @OtherLog(logName = "删除")
    public void delete(Long id) {
        productMapper.delete(id);
    }

    @Override
    @OtherLog(logName = "id查询")
    public Product queryById(Long id) {
        return productMapper.queryById(id);
    }

    @Override
    @OtherLog(logName = "查询")
    public List<Product> queryAll() {
        return productMapper.queryAll();
    }

    @Override
    @OtherLog(logName = "统计")
    public Long count(ProductQuery productQuery) {
        return productMapper.count(productQuery);
    }

    @Override
    @OtherLog(logName = "11")
    public List<Product> queryDate(ProductQuery productQuery) {
        return productMapper.queryDate(productQuery);
    }

    @Override
    @Transactional
    @OtherLog(logName = "批量删除")
    public void batchDel(Long[] ids) {
        productMapper.batchDel(ids);
    }

    @Override
    public Product getDetail(Long id) {
        return productMapper.getDetail(id);
    }

    @Override
    public PageInfo<Product> queryPage(ProductQuery query) {
        Long count = productMapper.count(query);
        List<Product> list = productMapper.queryDate(query);
        return new PageInfo<>(count, list);
    }
}